const request=require("superagent");
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
const util = require('util');
const charset = require("superagent-charset")(request);
const fs=require('fs');
var mysql      = require('mysql');
const assert = require('assert');
var connection = mysql.createConnection({
  host     : '127.0.0.1',
  user     : 'root',
  password : '123456',
  database : 'node'
});
connection.query = util.promisify(connection.query);
async function getResourceTree(page) {
    var resource = await page._client.send('Page.getResourceTree');
    return resource.frameTree;
}

async function getResourceContent(page, url) {
    const { content, base64Encoded } = await page._client.send(
        'Page.getResourceContent',
        { frameId: String(page.mainFrame()._id), url },
    );
    assert.equal(base64Encoded, true);
    return content;
};
const Agent=[
 	"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
    "Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
    "MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
    "JUC (Linux; U; 2.3.7; zh-cn; MB200; 320*480) UCWEB7.9.3.103/139/999",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0a1) Gecko/20110623 Firefox/7.0a1 Fennec/7.0a1",
    "Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10",
    "Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13",
    "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/1A542a Safari/419.3",
    "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7",
    "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10",
    "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+",
    "Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0",
    "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)",
    "Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; BLA-AL00 Build/HUAWEIBLA-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.9 Mobile Safari/537.36",
];
const timeout={
	response:50000,
	deadline:600000,
}
Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
function delay(t){
    return new Promise(resolve => setTimeout(resolve, t))
}

function rnd(n, m){
    var random = Math.floor(Math.random()*(m-n+1)+n);
    return random;
}
function getRndIP(){
    var ip=[];
    for(var i=0;i<4;i++){
    ip.push(rnd(10,250));
    }
    return ip.join(".");
}
//封装请求 实现失败重试
async function requestWithRetry(url, retryCount = 3, timeout = 5000,option) {
  return new Promise((resolve, reject) => {
    let numRetries = 0;
 
    const makeRequest = () => {
      request
        .get(url)
        .set(option)
        .timeout(timeout) // 设置超时时间
        .then(response => {
          resolve(response);
        })
        .catch(error => {
          if (error.code === 'ECONNABORTED' && numRetries < retryCount) {
            numRetries += 1;
            console.log(`Request timed out. Retrying... (Attempt ${numRetries})`);
            makeRequest(); // 重试请求
          } else {
            reject(error);
          }
        });
    };
 
    makeRequest();
  });
}
async function getList(){

	let sql=`select * from m_matter_a where id > 0`;
	let result=await connection.query(sql);
	for (var i = 0; i < result.length; i++) {
		//情形id
		let case_id = 0;
		let result_case=await connection.query(`select id from m_matter_case WHERE matter_id='${result[i]["id"]}'`);
		if(result_case.length>0){
			case_id = result_case[0]['id'];
		}else{
			case_id = await asySql("INSERT INTO m_matter_case SET  ?",{
						'pid':0,
						'matter_id':result[i]['id'],
						'name':'通用情形'
					}).then(insertId => {
						return insertId;
					}).catch(error => {
					  console.error(error); // 异步操作失败，输出错误信息
					});
		}
		
		if(case_id>0){
			var ip=getRndIP();
			var agent=Agent[rnd(0,Agent.length-1)];
			agent=Agent[0];
			var option={
				"Host":"zwfw.nmg.gov.cn",
				"User-Agent":agent,
				"Referer":'https://zwfw.nmg.gov.cn/',
				"X-Forwarded-For":ip,
			}
			//材料列表
			let url=`https://zwfw.nmg.gov.cn/handle_affairs_detail_ajaxMaterial?itemId=${result[i]['ItemID']}`;
			console.log(case_id,result[i]['id'],result[i]['name'],url);
			await delay(500);
			let res = await requestWithRetry(url, 3, 5000,option).then(response => {
			    // console.log('Response received:', response);
			    return response;
			  }).catch(error => {
			    console.error('Error:', error);
			  });
			let resData = JSON.parse(res.text).materials;
			for (var j = 0; j < resData.length; j++) {
				console.log(resData[j]["MaterialName"])
			  	let sql=`select id from m_matter_material WHERE MaterialCode='${resData[j]["MaterialCode"]}'`;
				let result_material=await connection.query(sql);
				if(result_material.length>0){
					console.log(`add ${resData[j]["MaterialCode"]} error`);
				}else{

					//类型
					let type = resData[j]["MaterialType"];
					if(type=='原件'){
						type = 1;
					}else if(type=='复印件'){
						type = 2;
					}else if(type=='原件和复印件'){
						type = 3;
					}else if(type=='电子版'){
						type = 4;
					}else{
						console.log('找不到类型',type)
						return;
					}

					//形式
					let shape = resData[j]["MaterialForm"];
					if(shape=='纸质'){
						shape = 1;
					}else if(shape=='电子'){
						shape = 2;
					}else if(shape=='纸质、电子'){
						shape = 3;
					}else{
						console.log('找不到形式',shape)
						return;
					}

					let MaterialForm = resData[j]["MaterialForm"];
					let number = 0;
					if(MaterialForm != '电子'){
						number = resData[j]["MaterialNumber"]
					}

					let necessity = resData[j]["Must"];
					if(necessity=='必须'){
						necessity = 1;
					}else if(necessity=='非必须'){
						necessity = 2;
					}else if(necessity=='信用免交、信用后补'){
						necessity = 3;
					}else if(necessity=='容缺后补'){
						necessity = 4;
					}else{
						console.log('找不到必要性',necessity)
						return;
					}

					let source = resData[j]["MaterialSource"];
					let source_info = '';
					if(source=='申请人自备'){
						source = 1;
					}else if(source=='政府部门核发'){
						source = 2;
						source_info = resData[j]["OtherMaterialSource"];
					}else if(source=='其他'){
						source = 3;
					}else{
						console.log('找不到来源渠道',source)
						return;
					}

					let exempt = 2;
					if(resData[j]["IsRelateElectronicLicense"]==0&&resData[j]["CERTIFICATE_HANDLING"]==0){
						exempt = 1;
					}

					let addSql="INSERT INTO m_matter_material SET  ?";
					let addSqlParams = {
						'case_id':case_id,
						'MaterialCode':resData[j]["MaterialCode"],
						'name':resData[j]["MaterialName"],
						'type':type,
						'shape':shape,
						'number':number,
						'necessity':necessity,
						'source':source,
						'source_info':source_info,
						'exempt':exempt,
						'notice':resData[j]["MaterialExplain"],
						'standard':resData[j]["AcceptanceCriteria"]
					};
					await asySql(addSql,addSqlParams).then(insertId => {
						return insertId;
					}).catch(error => {
					  console.error(error); // 异步操作失败，输出错误信息
					});
				}

			  	

			}
		}
		
			
			// await addDb(data,result[i]['id']);

		
		
		
	}



		
}


//插入数据库
async function asySql(addSql,addSqlParams){
	return await new Promise(resolve => {
		connection.query(addSql,addSqlParams,function (err, result) {
	        if(err){
	         console.log('[INSERT ERROR] - ',err.message);
	         return;
	        }else{
	        	console.log('INSERT ID:',result.insertId);
		    	return resolve(result.insertId);
	        }        
	 		
		});
	})
		
}
getList();
